#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h> // For PATH_MAX (might need unistd.h for realpath on some systems)
#include <unistd.h> // For realpath, sleep
#include "plugin_manager.h"

int main() {
    // 1. 获取可执行文件的绝对路径 (保持不变)
    char executable_path_full[PATH_MAX];
    // Using readlink is often more portable than realpath("/proc/self/exe")
    ssize_t len = readlink("/proc/self/exe", executable_path_full, sizeof(executable_path_full) - 1);
    if (len == -1) {
        perror("readlink /proc/self/exe failed");
        // Fallback or alternative method might be needed depending on OS
        // For simplicity, we'll try realpath as a fallback if needed,
        // but readlink is generally preferred for this specific task.
        if (realpath("/proc/self/exe", executable_path_full) == NULL) {
             perror("realpath /proc/self/exe also failed");
             return 1;
        }
    } else {
        executable_path_full[len] = '\0'; // Null-terminate the path read by readlink
    }


    // 2. 获取可执行文件所在的目录 (保持不变)
    char* last_slash = strrchr(executable_path_full, '/');
    if (last_slash == NULL) {
        fprintf(stderr, "Invalid executable path format.\n");
        return 1;
    }
    *last_slash = '\0'; // Truncate to get the directory path

    // 3. 构建插件目录和配置目录的完整路径 (保持不变)
    char plugin_dir[PATH_MAX];
    char config_dir[PATH_MAX];
    // Use the directory obtained above
    snprintf(plugin_dir, sizeof(plugin_dir), "%s/../lib/cpluginframework", executable_path_full); // Adjusted path relative to build/
    snprintf(config_dir, sizeof(config_dir), "%s/../configs", executable_path_full); // Adjusted path relative to build/

    printf("Executable directory: %s\n", executable_path_full);
    printf("Plugin directory: %s\n", plugin_dir);
    printf("Config directory: %s\n", config_dir);


    // 4. 创建插件管理器 (现在会自动加载 config.mp 中的插件)
    printf("Creating plugin manager...\n");
    PluginManager* manager = create_plugin_manager(plugin_dir, config_dir);
    if (!manager) {
        fprintf(stderr, "Failed to create plugin manager.\n");
        return 1;
    }
    printf("Plugin manager created. %d plugins loaded initially (based on config).\n", manager->plugin_count);


    // --- 移除手动加载特定插件的代码 ---
    /*
    PluginHandle plugin_handle = load_plugin(manager, "example_plugin");
    if (!plugin_handle) {
        fprintf(stderr, "Failed to load plugin.\n");
        destroy_plugin_manager(manager);
        return 1;
    }
    */

    // 5. 启动所有由管理器加载的插件
    start_all_plugins(manager);

    // 6. 主应用程序逻辑
    //    这里可以是一个事件循环、服务监听、或者只是等待一段时间
    printf("\nMain application running...\n");
    printf("Press Ctrl+C to exit.\n\n");
    // Example: Run for 10 seconds
    // sleep(10);
    // Or run indefinitely until interrupted
     while (1) {
         // Keep the main thread alive, plugins might be running in other threads
         sleep(1);
         // Add application logic here if needed
     }
    // If not using an infinite loop, ensure proper signal handling for graceful shutdown

    printf("\nShutting down...\n");

    // 7. 停止所有插件
    stop_all_plugins(manager);

    // --- 移除手动卸载特定插件的代码 ---
    /*
    unload_plugin(manager, plugin_handle);
    */

    // 8. 销毁插件管理器 (会负责卸载所有插件和清理资源)
    destroy_plugin_manager(manager);

    printf("Exiting application.\n");
    return 0;
}
